package com.fantasy.dao.impl;

import com.fantasy.dao.BaseDao;
import com.fantasy.dao.HistoryDao;
import com.fantasy.entity.Favorites;
import com.fantasy.entity.History;
import com.fantasy.entity.Video;

import java.util.List;

public class HistoryDaoImpl extends BaseDao implements HistoryDao {
    @Override
    public int insert(History history) {
        String sql = "INSERT INTO `history`(`user_id`,`video_id`) VALUE(?,?)";
        return super.update(sql,history.getUserId(),history.getVideoId());
    }

    @Override
    public int delete(Long video_id) {
        String sql = "DELETE FROM `history` WHERE `video_id`=?";
        return super.update(sql,video_id);
    }

    @Override
    public Integer queryForPageTotalCount(int user_id) {
        String sql = "SELECT COUNT(1) FROM `video` WHERE `id` = ANY (SELECT `video_id` FROM `history` WHERE `user_id` = ?)";
        Number count = (Number)queryForSingleValue(sql,user_id);
        return count.intValue();
    }

    @Override
    public List<Video> queryForPageItems(int user_id, int begin, int pageSize) {
        String sql = "SELECT * FROM `video` WHERE `id` = ANY (SELECT `video_id` FROM `history` WHERE `user_id` = ?) LIMIT ?,?";
        return super.queryForList(Video.class,sql,user_id,begin,pageSize);
    }
}
